<template >
    <a-button type="secondary" @click="execlOpen()" v-auth="['document:shipment:execlFile']">
        <template #icon>
        <icon-upload/>
        </template>
        上传附件
    </a-button>
    <add-modal ref="addModalRef"></add-modal>
</template>

<script setup lang="jsx">
import {computed, ref,inject,watch,reactive} from "vue";
import shipment from '@/api/document/shipment'
import AddModal from "@cps/actions/addModal.vue";
import {Message, Modal} from '@arco-design/web-vue'
import uploadConfig from '@/config/upload'
import commonApi from '@/api/common'
import tool from '@/utils/tool'
const parentCrudRef = inject('crudRef')
const props = defineProps({})
const addModalRef = ref()
const tableList = inject('tableList');
const execlOpen = ()=>{
  addModalRef.value.open(reactive([
    {
    formType: 'grid-tailwind',
    colNumber: 2,
    cols: [
      {
        formList: [
          {
              title: '附件类型',
              dataIndex: 'type',
              formType: 'select',
              rules: [{ required: true, message: '请选择附件类型' }],
              dict:{name: 'shipmenExeclType', translation: true,},
          }
        ],
      },
      {
        formList: [
          {
            title: '下载模板',
            dataIndex: 'model',
            formType: 'button',
            onClick: (from) => {
              if(!from.formModel.value.type){
                Message.error('请选择附件类型')
                return
              }
              downloadImportTemplate(from.formModel.value.type)
            }
          },
        ]
      }
    ]
  },
  {
      title: "文件",
      dataIndex: "file",
      type: 'file',
      formType: "upload",
      required: true,
    },
  ]), {
    viewType: 'modal',
  }, shipment.execlFile, '上传附件', function () {
    tableList()
    // parentCrudRef.value.refresh()
  })
}
const downloadImportTemplate = async (type) => {
    let res = await shipment.downloadTemplate({type:type})
    let url = res.data.url
    let storeMode = 'OSS'
  
    Message.info('处理中...')
    const domain = uploadConfig.storage[storeMode]
    url = domain + url
    
    const response = await commonApi.download(url, 'get')
    if (response) {
      tool.download(response, (storeMode !== 'LOCAL') ? res.data.origin_name : '')
      Message.success('请求成功，文件开始下载')  
    } else {
      Message.error('文件下载失败')  
    }
  }
</script>